package com.zhugang.sort;

import java.util.Arrays;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * @program algorithms
 * @description: sort
 * @author: chanzhugang
 * @create: 2022/02/28 22:09
 */
public class SortAlgo {

    public static void main(String[] args) {
        SortAlgo sortAlgo = new SortAlgo();
        int[] a = new int[]{3, 6, 9, 2, 5, 7, 10};
        //   sortAlgo.insertSort(a, a.length);
        //   sortAlgo.selectSort(a, a.length);
        //   sortAlgo.bubbleSort(a, a.length);
        //   sortAlgo.mergeSort(a, a.length);
        System.out.println(Arrays.toString(a));

        List<Integer> result = sortAlgo.quickSort(Arrays.asList(3, 6, 9, 2, 5, 7, 10));
        for (Integer res : result) {
            System.out.println(res);
        }

    }

    /**
     * 插入排序：找到要插入元素的位置，如果大于该元素，数据往后搬移
     *
     * @param a
     * @param n
     */
    public void insertSort(int[] a, int n) {
        if (n <= 1) {
            return;
        }
        for (int i = 0; i < n; i++) {
            int val = a[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (a[j] > val) {
                    a[j + 1] = a[j];
                } else {
                    break;
                }
            }
            a[j + 1] = val;
        }
    }


    /**
     * 选择排序：找未排序最小值放在已排序末尾（外层循环i）
     *
     * @param a
     * @param n
     */
    public void selectSort(int[] a, int n) {
        if (n <= 1) {
            return;
        }
        for (int i = 0; i < n - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < n; j++) {
                if (a[j] < a[minIndex]) {
                    minIndex = j;
                }
            }
            //交换位置，最小值放在有序末尾
            int temp = a[i];
            a[i] = a[minIndex];
            a[minIndex] = temp;
        }
    }

    /**
     * 冒泡排序：交换相邻元素位置
     *
     * @param a
     * @param n
     */
    public void bubbleSort(int[] a, int n) {
        if (n <= 1) {
            return;
        }
        for (int i = 0; i < n; i++) {
            boolean flag = false;
            for (int j = 0; j < n - i - 1; j++) {
                if (a[j] > a[j + 1]) {
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                    flag = true;
                }
            }
            // 没有要交换的元素
            if (!flag) break;
        }
    }

    /**
     * 找中间值，将数组分成两个有序数组，再合并成一个有序数组
     *
     * @param a
     * @param n
     */
    public void mergeSort(int[] a, int n) {
        if (n <= 1) {
            return;
        }
        mergeSort(a, 0, n);
    }

    private void mergeSort(int[] a, int l, int r) {
        if (r - l <= 1) {
            return;
        }
        int mid = (l + r) / 2;
        mergeSort(a, l, mid);
        mergeSort(a, mid, r);
        merge(a, l, mid, r);
    }

    private void merge(int[] a, int l, int mid, int r) {
        int[] left = Arrays.copyOfRange(a, l, mid + 1);
        int[] right = Arrays.copyOfRange(a, mid, r + 1);
        left[left.length - 1] = right[right.length - 1] = Integer.MAX_VALUE;
        int i = 0, j = 0;
        for (int k = l; k < r; k++) {
            if (left[i] < right[j]) {
                a[k] = left[i++];
            } else {
                a[k] = right[j++];
            }
        }
    }

    /**
     * 随机找一个数，小于放左边，大于放右边
     *
     * @param a
     * @return
     */
    public List<Integer> quickSort(List<Integer> a) {
        return quick(a);

    }

    private List<Integer> quick(List<Integer> a) {
        if (a.size() <= 1) {
            return a;
        }
        // 随机找一个数
        int val = a.get(0);
        List<Integer> left = a.stream().filter(data -> data < val).collect(toList());
        List<Integer> mid = a.stream().filter(data -> data == val).collect(toList());
        List<Integer> right = a.stream().filter(data -> data > val).collect(toList());

        left = quick(left);
        right = quick(right);
        left.addAll(mid);
        left.addAll(right);
        return left;
    }

    // 桶排序
}